#include "threadPool.h"

//接受一个TLV结构体并处理（选择调用函数）
//要把ssize_t length改为uint32_t length
int tlvResponse(int netfd, uint8_t type,ssize_t length,char *value){
    printf("TLVRESOPESE开始处理TLV响应\n");
    TLV_t *tlv = (TLV_t *)malloc(sizeof(TLV_t));
    memset(tlv,0,sizeof(tlv));
    if (!tlv) {
        LOG(LOG_LEVEL_ERROR, "system", "Memory allocation failed for TLV_t");
        printf("Memory allocation failed for TLV_t\n");
        return -1;
    }
    
    tlv->type = type;
    tlv->length = (uint32_t)length;
    tlv->seq = 0; // 假设序列号为0，实际应用中可能需要根据情况设置
    if(tlv->length!=0){
        memcpy(tlv->value, value, length);
        tlv->value[length]='\0';
    }else{
        tlv->value[0]='\0';
    }
    int ret = sendTlv(netfd, tlv);
    printf("TLVRESPONS_EOVER[type=0x%02X, len=%u, seq=%u, value=%s]\n", 
       tlv->type, tlv->length, tlv->seq, 
        tlv->value);
    free(tlv);
    
    return ret;
}


int chooseOptin(TLV_t *tlv, int netfd, pathStack_t *pstack,MYSQL *pthreadMysql,fileMetaData_t * pthreadFileMeta) {
    //printf("CHOOSEOPTION开始选择处理命令\n");
    printf("TLVCHOOSE_BEFIN[type=0x%02X, len=%u, seq=%u, value=%s]\n", 
       tlv->type, tlv->length, tlv->seq, 
        tlv->value);
    // printf("choooseoption|filemeta  filename is %s and path is %s\n",pthreadFileMeta->filename,pthreadFileMeta->path);
    // prinft("psatck top is %s,top_r is %s\n",pstack->stack[pstack->top],pstack->stack[pstack->top_r]);
    int pthread_user_id=-1;

    if (!tlv || !pstack) {
        LOG(LOG_LEVEL_ERROR, "system", "tlv or pstack is NULL");
        return -1;
    }
    
    // 处理命令
    //用于保存返回给客户端的内容
    char buffer[1024] = {0};
    int ret = 0;
    char filename[256] = {0};
    bzero(pthreadFileMeta->path,sizeof(pthreadFileMeta->path));
    memcpy(pthreadFileMeta->path,tlv->value,strlen(tlv->value));
    switch (tlv->type) {
        case CMD_CD:
            memset(buffer, 0, sizeof(buffer)); // 清空buffer
            if(pri_cd(tlv->seq,pthreadFileMeta, pstack)!=0){

                return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }else{
                return tlvResponse(netfd, CMD_RESPONSE,0,NULL);
            }
            //memcpy(buffer,tlv->value, );
        case CMD_FILE_CHECK:
            memset(buffer, 0, sizeof(buffer)); // 清空buffer
            if(check_file_hash(netfd,tlv,pthreadMysql)!=0){

                return tlvResponse(netfd, CMD_FILE_EXISTS, 0, NULL);
            }else{
                return tlvResponse(netfd, CMD_RESPONSE,0,NULL);
            }
        case CMD_LS:
            memset(buffer, 0, sizeof(buffer)); // 清空buffer
            if ( tlv->length == 0) {
                ret = pri_ls(tlv->seq, pthreadFileMeta, pstack,buffer);
            }else{
                ret = pri_ls(tlv->seq, pthreadFileMeta, pstack, buffer);
            }
            if (ret == 0) {
                return tlvResponse(netfd, CMD_RESPONSE, strlen(buffer), buffer);
            } else {
                return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }
        case CMD_PWD: 
            memset(buffer, 0, sizeof(buffer)); // 清空buffer  
            //ret = pwd_client(buffer, pstack);
            ret = pri_pwd(buffer,pstack);
            
            if(ret == 0){  // 使用之前保存的返回值
            return tlvResponse(netfd, CMD_RESPONSE, strlen(buffer), buffer);
            } else {
            return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }

        case CMD_PUTS:
            //memset(buffer, 0, sizeof(buffer)); 
            tlvResponse(netfd,CMD_RESPONSE, 0, NULL);
            recvFile(netfd, tlv,pstack,pthreadMysql,pthreadFileMeta);
            return 0;
        case CMD_GETS:
            //memset(buffer, 0, sizeof(buffer)); 
            tlvResponse(netfd,CMD_RESPONSE, 0, NULL);
            strncpy(filename, tlv->value, sizeof(filename) - 1);
            filename[sizeof(filename) - 1] = '\0'; // 确保字符串以'\0'结尾
            return getFile(netfd, tlv,pstack);
        case CMD_MKDIR:
            ret = pri_mkdir(tlv->seq, pthreadFileMeta, pstack);
            if (ret == 0) {
                return tlvResponse(netfd, CMD_RESPONSE, 0, NULL);
            } else {
                return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }
        case CMD_REMOVE:
            ret = pri_remove(tlv->seq,pthreadFileMeta, pstack);
            if (ret == 0) {
                return tlvResponse(netfd, CMD_RESPONSE, 0, NULL);
            } else {
                return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }
        case CMD_RMDIR: 
            ret = pri_rmdir(tlv->seq, pthreadFileMeta, pstack);
            if (ret == 0) {
                return tlvResponse(netfd, CMD_RESPONSE, 0, NULL);
            } else {
                return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }
        // case CMD_REGISTER:
        //     pthread_user_id = tackle_register(netfd,pthreadMysql,tlv);
        //     if(pthread_user_id==-1){
        //         printf("客户端注册失败\n");
        //         return -1;
        //     }
        //     pthreadFileMeta->owner_id = pthread_user_id;
        //     //char *ppath="/";
        //     strcpy(pthreadFileMeta->path,"/");
        //     strcpy(pthreadFileMeta->filename,"/");
        //     pthreadFileMeta->file_type=1;
        //     pthreadFileMeta->parent_directory_id=-1;
        //     pstack->mysql = pthreadMysql;
        //     pstack->owner_id = pthread_user_id;
        //     //stackInit(pstack);
        //     insert_into(pthreadMysql,pthreadFileMeta);
        //     stackInit(pstack);
        //     return 0;
        // case CMD_SIGNIN:
        //     pthread_user_id = tackle_signin(netfd,pthreadMysql,tlv);
        //     if(pthread_user_id==-1){
        //         printf("客户端登录失败\n");
        //         return -1;
        //     }
        //     pthreadFileMeta->owner_id = pthread_user_id;
        //     pstack->mysql = pthreadMysql;
        //     pstack->owner_id = pthread_user_id;
        //     stackInit(pstack);
        //     return 0;
        default:
            printf("未找到命令\n");
            LOG(LOG_LEVEL_WARN, "system", "Unknown command type: 0x%X", tlv->type);
            return -1;
    }
}

